home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / netprn / netprint.frm < prev    next >
Text File  |  1995-05-08  |  6KB  |  178 lines

  1. VERSION 2.00
  2. Begin Form Form1 
  3.    Caption         =   "NetPrint"
  4.    ClientHeight    =   3900
  5.    ClientLeft      =   2625
  6.    ClientTop       =   1635
  7.    ClientWidth     =   3840
  8.    Height          =   4305
  9.    Icon            =   NETPRINT.FRX:0000
  10.    Left            =   2565
  11.    LinkMode        =   1  'Source
  12.    LinkTopic       =   "Form1"
  13.    ScaleHeight     =   3900
  14.    ScaleWidth      =   3840
  15.    Top             =   1290
  16.    Width           =   3960
  17.    Begin Frame Frame1 
  18.       Caption         =   "Linked Printers"
  19.       Height          =   1872
  20.       Left            =   120
  21.       TabIndex        =   0
  22.       Top             =   1920
  23.       Width           =   3612
  24.       Begin CommandButton Disconnect 
  25.          Caption         =   "&Disconnect"
  26.          Height          =   372
  27.          Left            =   2400
  28.          TabIndex        =   5
  29.          Top             =   720
  30.          Width           =   1092
  31.       End
  32.       Begin ListBox LinkedPrinters 
  33.          Height          =   1395
  34.          Left            =   120
  35.          Sorted          =   -1  'True
  36.          TabIndex        =   4
  37.          Top             =   300
  38.          Width           =   2175
  39.       End
  40.    End
  41.    Begin Frame Frame2 
  42.       Caption         =   "Available Printers and Ports"
  43.       Height          =   1752
  44.       Left            =   120
  45.       TabIndex        =   6
  46.       Top             =   60
  47.       Width           =   3612
  48.       Begin CommandButton Connect 
  49.          Caption         =   "&Connect"
  50.          Height          =   372
  51.          Left            =   2400
  52.          TabIndex        =   3
  53.          Top             =   1260
  54.          Width           =   1092
  55.       End
  56.       Begin ListBox PrintPorts 
  57.          Height          =   1005
  58.          Left            =   2400
  59.          Sorted          =   -1  'True
  60.          TabIndex        =   2
  61.          Top             =   240
  62.          Width           =   1095
  63.       End
  64.       Begin ListBox NetPrinters 
  65.          Height          =   1395
  66.          Left            =   120
  67.          Sorted          =   -1  'True
  68.          TabIndex        =   1
  69.          Top             =   240
  70.          Width           =   2175
  71.       End
  72.    End
  73. End
  74. 'NetPrint has been placed in the public domain by Art Krumsee
  75. 'CompuServe ID 76702,1526.  Internet address art.krumsee@osu.edu
  76.  
  77. Declare Function WNetGetConnection Lib "User" (ByVal LocalDev$, ByVal rmtname$, buffsize%) As Integer
  78. Declare Function WNetAddConnection Lib "User" (ByVal NetPath As String, ByVal Password As String, ByVal LocalDev As String) As Integer
  79. Declare Function WNetCancelConnection Lib "User" (ByVal LocalDev As String, ByVal Force As Integer) As Integer
  80. Declare Function GetPrivateProfileString Lib "Kernel" (ByVal lpApplicationName As String, KeyName As Any, Default As Any, ByVal lpReturnedString As String, ByVal nSize As Integer, ByVal lpFileName As String) As Integer
  81. Declare Function SendMessage% Lib "User" (ByVal hWnd%, ByVal wMsg%, ByVal wParam%, ByVal lParam&)
  82. Declare Function GetFocus% Lib "User" ()
  83. Declare Function PutFocus% Lib "User" Alias "SetFocus" (ByVal hWnd%)
  84. Const WM_USER = &H400
  85. Const LB_RESETCONTENT = WM_USER + 5
  86.  
  87. Sub ClearListBox (Ctrl As Control)
  88.     hWndOld% = GetFocus()
  89.     Ctrl.SetFocus
  90.     X = SendMessage(GetFocus(), LB_RESETCONTENT, 0, 0)
  91.     Suc% = PutFocus(hWndOld%)
  92. End Sub
  93.  
  94. Sub Connect_Click ()
  95.     'x% = MsgBox(printports.text)
  96.     If netprinters.listindex = -1 Or printports.listindex = -1 Then
  97.         X% = MsgBox("You must select a printer and a port and then press Connect.")
  98.     Else
  99.         Link_Printer$ = netprinters.text
  100.         Link_Port$ = printports.text
  101.         X% = WNetAddConnection(Link_Printer$, "", Link_Port$)
  102.         Select Case X%
  103.         Case 0
  104.             ClearListBox linkedPrinters
  105.             ClearListBox printports
  106.             findlinks
  107.         Case 6
  108.             n% = MsgBox("Password required.  Could not connect printer.")
  109.         Case 52
  110.             n% = MsgBox("Network Connection to that port already exists.")
  111.         Case Else
  112.             n% = MsgBox("Network Link Failed")
  113.         End Select
  114.     End If
  115. End Sub
  116.  
  117. Sub Disconnect_Click ()
  118.     'x% = MsgBox(printports.text)
  119.     If linkedPrinters.listindex = -1 Then
  120.         X% = MsgBox("You must select a linked printer and then press Disconnect.")
  121.     Else
  122.         Linked_Port$ = Left$(linkedPrinters.text, 5)
  123.         X% = WNetCancelConnection(Linked_Port$, 0)
  124.         Select Case X%
  125.         Case 0
  126.             ClearListBox linkedPrinters
  127.             ClearListBox printports
  128.             findlinks
  129.         Case 48
  130.             n% = MsgBox("This printer port was not connected to a network device.")
  131.         Case 49
  132.             n% = MsgBox("Open files prevented the completion of this operation")
  133.         Case Else
  134.             n% = MsgBox("Network Disconnect Failed")
  135.         End Select
  136.     End If
  137.     
  138. End Sub
  139.  
  140. Sub findlinks ()
  141.     'Find out which printer ports are already linked
  142.     For X = 1 To 4
  143.         n% = 0
  144.         rmtname$ = String$(255, 0)
  145.         LocalDev$ = "LPT" + LTrim$(Str$(X)) + ":"
  146.         n% = WNetGetConnection(LocalDev$, rmtname$, 255)
  147.         If n% = 0 Then
  148.             rmtname$ = Left$(rmtname$, InStr(rmtname$, Chr$(0)) - 1)
  149.             linkedPrinters.AddItem LocalDev$ + "  " + rmtname$
  150.         Else
  151.             printports.AddItem LocalDev$
  152.         End If
  153.     Next X
  154.     printports.Refresh
  155.  
  156. End Sub
  157.  
  158. Sub Form_Load ()
  159.  
  160.     'NetPrint has been placed in the public domain by Art Krumsee
  161.     'CompuServe ID 76702,1526.  Internet address art.krumsee@osu.edu
  162.     
  163.     findlinks
  164.     'Get list of available printers from NETPRINT.ini
  165.     ret$ = String$(255, 0)
  166.     App_Name$ = "Netprint"
  167.     Key_Val = 0&
  168.     Def_Val$ = "No Printers Defined"
  169.     Ini_Name$ = "netprint.ini"
  170.     n% = GetPrivateProfileString(App_Name$, ByVal Key_Val, Def_Val$, ret$, 255, Ini_Name$)
  171.     While Left$(ret$, 1) <> Chr$(0)
  172.         rmtname$ = Left$(ret$, InStr(ret$, Chr$(0)) - 1)
  173.         netprinters.AddItem ret$
  174.         ret$ = Right$(ret$, Len(ret$) - InStr(ret$, Chr$(0)))
  175.     Wend
  176. End Sub
  177.  
  178.